package com.lyp.crm.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lyp.crm.dao.ModulesMapper;
import com.lyp.crm.entity.Fenye;
import com.lyp.crm.entity.Modules;
import com.lyp.crm.entity.Node;
import com.lyp.crm.entity.Rolesmodules;
@Service
public class ModulesdaoImp implements Modulesdao{
@Autowired
	private ModulesMapper modulesMapper;
@Autowired
	private Rolesmodules rolesmodules;
	@Override
	public Fenye<Modules> SelectModules(Fenye<Modules> fenye) {
		List<Modules> selectModules = modulesMapper.SelectModules(fenye);
		Integer selectModulesCount = modulesMapper.SelectModulesCount(fenye);
		fenye.setRows(selectModules);
		fenye.setTotal(selectModulesCount);
		return fenye;
	}

	@Override
	public Integer insertModules(Modules modules) {
		return modulesMapper.insertModules(modules);
	}

	@Override
	public Integer deleteModules(Integer mid) {
		return modulesMapper.deleteModules(mid);
	}

	@Override
	public Integer updateModules(Modules modules) {
		return modulesMapper.updateModules(modules);
	}

	@Override
	public List<Modules> mymodules(Integer rid) {
		// TODO Auto-generated method stub
		return modulesMapper.mymodules(rid);
	}

	@Override
	public Integer insertrolesmodules(Rolesmodules rolesmodules) {
		// TODO Auto-generated method stub
		return modulesMapper.insertrolesmodules(rolesmodules);
	}

	@Override
	public Integer selectrolesmodules(Rolesmodules rolesmodules) {
		// TODO Auto-generated method stub
		return modulesMapper.selectrolesmodules(rolesmodules);
	}

	@Override
	public Integer deletelesmodules(Integer rmid) {
		// TODO Auto-generated method stub
		return modulesMapper.deletelesmodules(rmid);
	}

	@Override
	public Integer selectbymidModules(Integer mid) {
		// TODO Auto-generated method stub
		return modulesMapper.selectbymidModules(mid);
	}

	@Override
	public Integer selectinsbymnanme(Modules modules) {
		// TODO Auto-generated method stub
		return modulesMapper.selectinsbymnanme(modules);
	}

	@Override
	public List<Node> selectRolesModulesTree(Integer rid) {
			// TODO Auto-generated method stub
			List<Node> mokuaiTreelist = new ArrayList<Node>();
			List<Modules> mokuailist = modulesMapper.SelectModuleslist();
			for (int i = 0; i < mokuailist.size(); i++) {
				Integer mfather = mokuailist.get(i).getMfather();
				if (mfather==0) {
					addtree(mokuaiTreelist, mokuailist, i, rid);
				}
			}
			return mokuaiTreelist;
		}


private void addtree(List<Node> mokuaiTreelist, List<Modules> mokuailist, int i, Integer rid) {
		Node mk = new Node();
		mk.setId(mokuailist.get(i).getMid());
		mk.setText(mokuailist.get(i).getMname());
		rolesmodules.setRmrid(rid);
		rolesmodules.setRmmid(mokuailist.get(i).getMid());
		Integer selectTrueRolesModules = modulesMapper.selectTrueRolesModules(rolesmodules);
		Integer mfather = mokuailist.get(i).getMfather();
		if (mfather==0) {
			Integer selectModulesChildren = modulesMapper.selectModulesChildren(mokuailist.get(i).getMid());
			if (selectModulesChildren > 0) {
				mk.setChecked(false);
			} else {
				mk.setChecked(selectTrueRolesModules == 1);
			}
		} else {
			mk.setChecked(selectTrueRolesModules == 1);
		}
		Node fortree = fortree(mokuailist, i, mk, rid);
		if (fortree != null) {
			mokuaiTreelist.add(fortree);
		}
		
	}
	private Node fortree(List<Modules> mokuailist, int i, Node mk, Integer rid) {
		List<Node> treelist = new ArrayList<Node>();
		for (int j = 0; j < mokuailist.size(); j++) {
			if (mokuailist.get(j).getMfather() == mokuailist.get(i).getMid()) {
				addtree(treelist, mokuailist, j, rid);
			}
		}
		mk.setChildren(treelist);
		return mk;
	}

	@Override
	public List<Modules> SelectModuleslist(Modules modules) {
		// TODO Auto-generated method stub
		return modulesMapper.SelectModuleslist();
	}

	@Override
	public Modules selectModulesmid(Integer mid) {
		// TODO Auto-generated method stub
		return modulesMapper.selectModulesmid(mid);
	}


}
